package com.ruoyi.vbze.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.vbze.mapper.TemperatureMapper;
import com.ruoyi.vbze.domain.Temperature;
import com.ruoyi.vbze.service.ITemperatureService;

/**
 * 气温Service业务层处理
 *
 * @author vbze
 * @date 2023-02-27
 */
@Service
public class TemperatureServiceImpl implements ITemperatureService {


    private static final Logger log = LoggerFactory.getLogger(TemperatureServiceImpl.class);

    @Autowired
    private TemperatureMapper temperatureMapper;

    /**
     * 查询气温
     *
     * @param id 气温主键
     * @return 气温
     */
    @Override
    public Temperature selectTemperatureById(Long id) {
        return temperatureMapper.selectTemperatureById(id);
    }

    /**
     * 查询气温列表
     *
     * @param temperature 气温
     * @return 气温
     */
    @Override
    public List<Temperature> selectTemperatureList(Temperature temperature) {
        return temperatureMapper.selectTemperatureList(temperature);
    }

    /**
     * 新增气温
     *
     * @param temperature 气温
     * @return 结果
     */
    @Override
    public int insertTemperature(Temperature temperature) {
        return temperatureMapper.insertTemperature(temperature);
    }

    /**
     * 修改气温
     *
     * @param temperature 气温
     * @return 结果
     */
    @Override
    public int updateTemperature(Temperature temperature) {
        return temperatureMapper.updateTemperature(temperature);
    }

    /**
     * 批量删除气温
     *
     * @param ids 需要删除的气温主键
     * @return 结果
     */
    @Override
    public int deleteTemperatureByIds(Long[] ids) {
        return temperatureMapper.deleteTemperatureByIds(ids);
    }

    /**
     * 删除气温信息
     *
     * @param id 气温主键
     * @return 结果
     */
    @Override
    public int deleteTemperatureById(Long id) {
        return temperatureMapper.deleteTemperatureById(id);
    }


    /**
     * 导入气温数据
     *
     * @param temperatureList 植株列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importTemperature(List<Temperature> temperatureList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(temperatureList) || temperatureList.size() == 0) {
            throw new ServiceException("导入气温数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (Temperature temperature : temperatureList) {
            try {
                // 验证是否存在这条数据
//                SysUser u = userMapper.selectUserByUserName(block.getUserName());
                Temperature t = temperatureMapper.selectTemperatureByStationAndDate(temperature.getStation(), temperature.getDate());
                if (StringUtils.isNull(t)) {
//                    BeanValidators.validateWithException(validator, block);
//                    block.setPassword(SecurityUtils.encryptPassword(password));
                    temperature.setCreateBy(operName);
//                    temperature.setLastEditor(operName);
                    temperature.setUpdateBy(operName);
                    this.insertTemperature(temperature);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " " + temperature.getName() + " " + sdf.format(temperature.getDate()) + " 的气温数据导入成功");
                } else if (isUpdateSupport) {
//                    BeanValidators.validateWithException(validator, block);
//                    checkUserAllowed(block);
//                    checkUserDataScope(block.getUserId());
//                    block.setUpdateBy(operName);
                    temperature.setUpdateBy(operName);
//                    temperature.setLastEditor(operName);
                    this.updateTemperature(temperature);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " " + temperature.getName() + " " + sdf.format(temperature.getDate()) + " 的气温数据更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + " " + temperature.getName() + " " + sdf.format(temperature.getDate()) + " 的气温数据已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + " " + temperature.getName() + " " + sdf.format(temperature.getDate()) + " 的气温数据导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
